package org.common.middlewave;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 在请求处理之前执行，例如检查用户登录状态
        System.out.println("Pre Handle - Custom Interceptor");

        System.out.println(request.getHeader("Authentication"));
        return true; // 返回true表示继续执行下一个拦截器或请求处理，返回false则中断
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 请求处理之后、视图渲染之前执行
        System.out.println("Post Handle - Custom Interceptor");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 整个请求处理完毕之后执行，包括异常处理
        System.out.println("After Completion - Custom Interceptor");
    }
}